package com.wan.service.impl;

import com.wan.config.PasswordUtil;
import com.wan.dao.UserMapper;
import com.wan.entity.User;
import com.wan.service.UserService;
import com.wan.utils.SystemConstant;
import com.wan.utils.UUIDUtils;
import com.wan.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param loginName
     * @param loginPwd
     * @return
     */

    @Override
    public User login(String loginName, String loginPwd) {
        //调用根据账号查询用户信息的方法
        User user = userMapper.findUserByLoginName(loginName);
        //判断对象是否为空
        if (user != null){
            //将密码加密处理
            String newPassword = PasswordUtil.md5(loginPwd,user.getSalt(), SystemConstant.PASSWORD_COUNT);
            //比较密码是否一致
            if (user.getLoginPwd().equals(newPassword)){
                return user;//登录成功
            }
        }
        //登录失败
        return null;
    }

    @Override
    public int getUserCountByDeptId(Integer deptId) {
        return userMapper.getUserCountByDeptId(deptId);
    }

    @Override
    public int getUserCountByRoleId(Integer roleId) {
        return userMapper.getUserCountByDeptId(roleId);
    }

    @Override
    public List<User> findUserList(UserVo userVo) {
        return userMapper.findUserList(userVo);
    }

    @Override
    public int addUser(User user) {
        user.setSalt(UUIDUtils.randomUUID());
        user.setCreateDate(new Date());//创建时间
        user.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,user.getSalt(),SystemConstant.PASSWORD_COUNT));
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        //设置修改时间
        user.setModifyDate(new Date());
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteById(Integer id) {
        //删除员工角色关系表的数据
        userMapper.deleteUserAndRole(id);
        //调用删除员工的方法
        return userMapper.deleteById(id);
    }

    @Override
    public int resetPwd(int id) {
        User user = new User();
        user.setSalt(UUIDUtils.randomUUID());//必须先设置盐值，再给密码加密
        user.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,user.getSalt(),SystemConstant.PASSWORD_COUNT));
        user.setId(id);//主键，员工编号
        return userMapper.updateUser(user);
    }

    @Override
    public boolean saveUserRole(String roleIds, Integer userId) {
        try {
            //先删除员工角色关系表的数据
            userMapper.deleteUserAndRole(userId);
            //再保存员工角色关系
            String [] idStr = roleIds.split(",");
            for (int i = 0; i < idStr.length; i++) {
                userMapper.addUserRole(idStr[i],userId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
